package com.migu.contacts.main.countdown;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static com.migu.contacts.net.Service.API;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.frank.libandroid.BaseActivity;
import com.frank.libandroid.BaseAdapter;
import com.frank.libandroid.BottomPopup;
import com.frank.libandroid.DataStore;
import com.frank.libandroid.butterknife.BindAdapter;
import com.frank.libandroid.butterknife.OnClick;
import com.lxj.xpopup.XPopup;
import com.migu.contacts.R;
import com.migu.contacts.bean.ContactBean;
import com.migu.contacts.bean.DetailBean;
import com.migu.contacts.bean.MomentBean;
import com.migu.contacts.bean.TimerBean;
import com.migu.contacts.databinding.ActivityEditMomentBinding;
import com.migu.contacts.databinding.ItemContactEditContact2Binding;
import com.migu.contacts.databinding.ItemCreateTodoBinding;
import com.migu.contacts.databinding.ItemCreateTodoImportanceBinding;
import com.migu.contacts.databinding.PopTodoImportanceBinding;
import com.migu.contacts.main.ContactSelectActivity;
import com.migu.contacts.main.todo.CreateTodoActivity;
import com.migu.contacts.net.ResultCallback;
import com.migu.contacts.util.GroupUtil;
import com.migu.contacts.util.ImportanceUtil;
import com.migu.contacts.widget.Label2;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class EditMomentActivity extends BaseActivity<ActivityEditMomentBinding> {
    TimerBean timerBean = (TimerBean) get();
    MomentBean momentBean;
    DetailBean detailBean;
    @BindAdapter(R.id.recyclerView)
    BaseAdapter<ItemContactEditContact2Binding, DetailBean.ContactInfoList> contactAdapter = new BaseAdapter<ItemContactEditContact2Binding, DetailBean.ContactInfoList>() {
        @Override
        protected void onBindData(ItemContactEditContact2Binding binding, DetailBean.ContactInfoList bean, int position) {
            super.onBindData(binding, bean, position);
            List<Integer> groupIdList = bean.groupList;
            binding.group.setVisibility(GONE);
            if (groupIdList != null && groupIdList.size() > 0) {
                Integer i = groupIdList.get(0);
                String groupName = GroupUtil.getGroupName(i);
                if (!TextUtils.isEmpty(groupName)) {
                    binding.group.setVisibility(VISIBLE);
                    binding.group.setText(groupName);
                }
            }
        }
    };

    public void goContacts(View view) {
        startActivityForResult(ContactSelectActivity.class, new Runnable() {
            @Override
            public void run() {
                Set<ContactBean> contactBeans = DataStore.get(ContactSelectActivity.KEY_CONTACTS);
                if (contactBeans != null) {
                    detailBean.contactInfoList.clear();
                    for (ContactBean contactBean : contactBeans) {
                        detailBean.contactInfoList.add(new DetailBean.ContactInfoList(contactBean));
                    }
                    if (detailBean.contactInfoList.isEmpty()) {
                        mBinding.selectPerson.setVisibility(View.VISIBLE);
                        mBinding.addContacts.setVisibility(View.GONE);
                    } else {
                        mBinding.selectPerson.setVisibility(View.GONE);
                        mBinding.addContacts.setVisibility(View.VISIBLE);
                    }
                    contactAdapter.setData(detailBean.contactInfoList);
                }
            }
        });
    }

    @BindAdapter(R.id.recyclerView2)
    BaseAdapter<ItemCreateTodoBinding, Label2.LabelBean> midAdapter = new BaseAdapter<ItemCreateTodoBinding, Label2.LabelBean>() {
        @OnClick(R.id.iv_arrow)
        public void iv_arrow(Label2.LabelBean bean, int position) {
            if (position == 0) {
//                showPopNotice(bean);
//            } else if (position == 1) {
//                showCyclePop(bean);
//            } else if (position == 2) {
                showImportancePop(bean);
            }
        }
    };

    private void showImportancePop(Label2.LabelBean bean) {
        BottomPopup bottomPopup = new BottomPopup<PopTodoImportanceBinding>(mActivity) {

            boolean isOpen = false;
            CreateTodoActivity.ImportanceBean checkedNotice = null;
            @BindAdapter(R.id.recyclerView)
            BaseAdapter<ItemCreateTodoImportanceBinding, CreateTodoActivity.ImportanceBean> noticeAdapter = new BaseAdapter<ItemCreateTodoImportanceBinding, CreateTodoActivity.ImportanceBean>() {
                @OnClick(R.id.checkBox)
                public void checkBox(CreateTodoActivity.ImportanceBean bean) {
                    Log.d(TAG, "checkBox() called with: bean = [" + bean.checked + "]");
                    if (bean.checked) {
                        isOpen = true;
                        checkedNotice = bean;
                        for (CreateTodoActivity.ImportanceBean noticeBean : list) {
                            noticeBean.checked = false;
                        }
                        bean.checked = true;
                        notifyDataSetChanged();
                        mBinding.noNotice.setChecked(false);
                    }
                }
            };

            @OnClick(R.id.close)
            public void quit() {
                dismiss();
            }

            @OnClick(R.id.noNotice)
            public void noNotice() {
                if (mBinding.noNotice.isChecked()) {
                    for (CreateTodoActivity.ImportanceBean noticeBean : list) {
                        noticeBean.checked = false;
                    }
                    noticeAdapter.notifyDataSetChanged();
                    isOpen = false;
                }
            }

            List<CreateTodoActivity.ImportanceBean> list = new ArrayList<>();

            //            重复周期类型 1=天 2=周 3=月 4=年
            @Override
            protected void initView() {
                super.initView();
                list.add(new CreateTodoActivity.ImportanceBean("高", R.drawable.label_h, 3));
                list.add(new CreateTodoActivity.ImportanceBean("中", R.drawable.label_m, 2));
                list.add(new CreateTodoActivity.ImportanceBean("低", R.drawable.label_l, 1));
                noticeAdapter.setData(list);
                mBinding.noNotice.setChecked(true);
            }

            @OnClick(R.id.confirm)
            public void confirm() {
//                mNoticeBean = checkedNotice;
                if (isOpen) {
                    bean.right = checkedNotice.name;
                    momentBean.momentsMatter = checkedNotice.value;
                } else {
                    momentBean.momentsMatter = 0;
                    bean.right = "无";
                }
                midAdapter.notifyDataSetChanged();
                dismiss();
            }

        };
        new XPopup.Builder(mActivity).asCustom(bottomPopup).show();
    }

    @Override
    protected void initData() {
        super.initData();
        mBinding.date.setBean(new Label2.LabelBean("目标日", LocalDate.now().toString(), 0, true));
        List<Label2.LabelBean> mid = new ArrayList<>();
        mid.add(new Label2.LabelBean("重要程度", "无", 0, true));
        midAdapter.setData(mid);
        API.getMomentDetail(timerBean.id).enqueue(new ResultCallback<DetailBean<MomentBean>>() {
            @Override
            protected void onSuccess(DetailBean<MomentBean> detailBean) {
                momentBean = detailBean.data;
                EditMomentActivity.this.detailBean = detailBean;
                mBinding.setBean(momentBean);
                if (momentBean != null) {
                    int momentsMatter = momentBean.momentsMatter;
                    if (momentsMatter > 0) {
                        midAdapter.getData().get(0).right = ImportanceUtil.getName(momentsMatter);
                        midAdapter.notifyItemChanged(0);
                    }
                }
                List<DetailBean.ContactInfoList> contactBeanList = detailBean.contactInfoList;
                if (contactBeanList == null) {
                    detailBean.contactInfoList = new ArrayList<>();
                    contactBeanList = detailBean.contactInfoList;
                }
                if (contactBeanList.size() > 0) {
                    mBinding.selectPerson.setVisibility(View.GONE);
                    mBinding.addContacts.setVisibility(View.VISIBLE);
                    contactAdapter.setData(contactBeanList);
                } else {
                    mBinding.selectPerson.setVisibility(View.VISIBLE);
                    mBinding.addContacts.setVisibility(View.GONE);
                }
            }

            @Override
            protected void onError(int code, String message) {
                super.onError(code, message);
                finish();
            }
        });
    }

    public void save(View view) {
        detailBean.save();
        API.calendarEdit(detailBean).enqueue(new ResultCallback<Boolean>() {
            @Override
            protected void onSuccess(Boolean aBoolean) {
                finish();
            }
        });
    }
}